//
//  HCSuperCRMModel.m
//  HelloCall
//
//  Created by WangYongpeng on 13-9-21.
//  Copyright (c) 2013年 HC. All rights reserved.
//

#import "HCSuperCRMModel.h"
#import "HCNetworkManger.h"

@implementation HCCRMCustomInfo

@synthesize userID = m_userID;
//@synthesize ID = m_ID;    
@synthesize customPhoneNumber = m_customPhoneNumber;     //用户号码
@synthesize mobilePhoneNumber = m_mobilePhoneNumber;     //联系手机
@synthesize phoneNumber = m_phoneNumber;                //座机号码
@synthesize contact = m_contact;                        //联系人
@synthesize company = m_company;                        //公司单位
@synthesize qqNumber = m_qqNumber;                      //QQ号
@synthesize email = m_email;                            //邮件
@synthesize web = m_web;                                //网站
@synthesize attribute = m_attribute;                    //客户属性
@synthesize nextVisitTime = m_nextVisitTime;            //下次沟通时间（单位:天）
@synthesize Memo = m_Memo;                              //备忘录
@synthesize initTime = m_initTime;                      //初识沟通时间
@synthesize sourceType = m_sourceType;                  //来源
@synthesize potentialCustomersTime = m_potentialCustomersTime; //转为潜在客户周期（单位：天）
@synthesize transactionTime = m_transactionTime;        //成交周期（单位:天）
@synthesize visitCount = m_visitCount;                  //沟通次数
@synthesize sumCallTime = m_sumCallTime;                //通过总时长(单位:分钟)
@synthesize reVisitTime = m_reVisitTime;                //回访周期（单位:天）
//@synthesize remarkInfoList = m_remarkInfoList;

- (void)dealloc
{
    self.userID = nil;
    self.customPhoneNumber = nil;
    self.mobilePhoneNumber = nil;
    self.phoneNumber = nil;
    self.contact = nil;
    self.company = nil;
    self.qqNumber = nil;
    self.web = nil;
    self.Memo = nil;
    self.initTime = nil;
//    self.remarkInfoList = nil;
    
    [super dealloc];
}

@end

@interface HCSuperCRMModel ()<HCNetworkObserver>

@property (nonatomic, retain)NSMutableArray* transactionArray;  //成交客户列表
@property (nonatomic, retain)NSMutableArray* potentialArray;    //潜在客户列表
@property (nonatomic, retain)NSMutableArray* followArray;       //继续跟进客户列表
@property (nonatomic, retain)NSMutableArray* giveUpArray;       //放弃客户列表
    
@end

@implementation HCSuperCRMModel

@synthesize transactionArray = m_transactionArray;
@synthesize potentialArray = m_potentialArray;
@synthesize followArray = m_followArray;
@synthesize giveUpArray = m_giveUpArray;
@synthesize staffID = m_staffID;
@synthesize workPhoneNumber = m_workPhoneNumber;

//@synthesize delegate = m_delegate;

static HCSuperCRMModel* g_defaultModel = nil;

+ (HCSuperCRMModel*)defaultModel
{
    if (nil == g_defaultModel)
    {
        g_defaultModel = [[HCSuperCRMModel alloc] init];
    }
    return g_defaultModel;
}

- (id)init
{
    self = [super init];
    if (nil != self)
    {
        self.transactionArray = [NSMutableArray arrayWithCapacity:0];
        self.potentialArray = [NSMutableArray arrayWithCapacity:0];
        self.followArray = [NSMutableArray arrayWithCapacity:0];
        self.giveUpArray = [NSMutableArray arrayWithCapacity:0];
        self.staffID = @"2910";
        self.workPhoneNumber = @"17612346789";
        
        [[HCNetworkManger shareManger] addObserver:self];
        
//        HCCRMCustomInfo* info = [[[HCCRMCustomInfo alloc] init] autorelease];
//        info.ID = 100;
//        info.customPhoneNumber = @"123456789";
//        info.mobilePhoneNumber = @"23456789";
//        info.phoneNumber = @"345678";
//        info.contact = @"张三";
//        info.company = @"中国电信";
//        info.qqNumber = @"456123897";
//        info.web = @"www.baidu.com";
//        info.attribute = ECustomAttribute_1;
//        info.nextVisitTime = 3;
//        info.Memo = @"备注";
//        info.initTime = [NSDate date];
//        info.sourceType = CallSourceType_allocation;
//        info.potentialCustomersTime = 4;
//        info.transactionTime = 4;
//        info.visitCount = 10;
//        info.sumCallTime = 500;
//        info.reVisitTime = 3;
//        HCCallRemarkInfo* remarkInfo = [[[HCCallRemarkInfo alloc] init] autorelease];
//        remarkInfo.addRemarkDate = [NSDate date];
//        remarkInfo.mobliePhoneNumber = @"123123";
//        NSMutableArray* remarkInfoArray = [NSMutableArray arrayWithCapacity:3];
//        [remarkInfoArray addObject:remarkInfo];
//        remarkInfo = [[[HCCallRemarkInfo alloc] init] autorelease];
//        remarkInfo.addRemarkDate = [NSDate date];
//        remarkInfo.mobliePhoneNumber = @"3456";
//        [remarkInfoArray addObject:remarkInfo];
//        
//        remarkInfo = [[[HCCallRemarkInfo alloc] init] autorelease];
//        remarkInfo.addRemarkDate = [NSDate date];
//        remarkInfo.mobliePhoneNumber = @"7896";
//        [remarkInfoArray addObject:remarkInfo];
//        info.remarkInfoList = remarkInfoArray;
//        [self.transactionArray addObject:info];
//        
//        info = [[[HCCRMCustomInfo alloc] init] autorelease];
//        info.ID = 101;
//        info.customPhoneNumber = @"456789";
//        info.mobilePhoneNumber = @"567890";
//        info.phoneNumber = @"678901";
//        info.contact = @"李四";
//        info.company = @"中国联通";
//        info.qqNumber = @"456123897";
//        info.web = @"www.baidu.com";
//        info.attribute = ECustomAttribute_1;
//        info.nextVisitTime = 3;
//        info.Memo = @"备注";
//        info.initTime = [NSDate date];
//        info.sourceType = CallSourceType_allocation;
//        info.potentialCustomersTime = 4;
//        info.transactionTime = 4;
//        info.visitCount = 10;
//        info.sumCallTime = 500;
//        info.reVisitTime = 3;
//        [self.transactionArray addObject:info];
//        
//        info = [[[HCCRMCustomInfo alloc] init] autorelease];
//        info.ID = 102;
//        info.customPhoneNumber = @"23456789";
//        info.mobilePhoneNumber = @"567890";
//        info.phoneNumber = @"678901";
//        info.contact = @"王五";
//        info.company = @"中国联通";
//        info.qqNumber = @"456123897";
//        info.web = @"www.baidu.com";
//        info.attribute = ECustomAttribute_1;
//        info.nextVisitTime = 3;
//        info.Memo = @"备注";
//        info.initTime = [NSDate date];
//        info.sourceType = CallSourceType_allocation;
//        info.potentialCustomersTime = 4;
//        info.transactionTime = 4;
//        info.visitCount = 10;
//        info.sumCallTime = 500;
//        info.reVisitTime = 3;
//        [self.potentialArray addObject:info];
//        
//        info = [[[HCCRMCustomInfo alloc] init] autorelease];
//        info.ID = 103;
//        info.customPhoneNumber = @"23456789";
//        info.mobilePhoneNumber = @"567890";
//        info.phoneNumber = @"678901";
//        info.contact = @"赵六";
//        info.company = @"中国石化";
//        info.qqNumber = @"456123897";
//        info.web = @"www.baidu.com";
//        info.attribute = ECustomAttribute_1;
//        info.nextVisitTime = 3;
//        info.Memo = @"备注";
//        info.initTime = [NSDate date];
//        info.sourceType = CallSourceType_allocation;
//        info.potentialCustomersTime = 4;
//        info.transactionTime = 4;
//        info.visitCount = 10;
//        info.sumCallTime = 100;
//        info.reVisitTime = 3;
//        [self.potentialArray addObject:info];
//        
//        info = [[[HCCRMCustomInfo alloc] init] autorelease];
//        info.ID = 104;
//        info.customPhoneNumber = @"23456789";
//        info.mobilePhoneNumber = @"567890";
//        info.phoneNumber = @"678901";
//        info.contact = @"郭七";
//        info.company = @"中国石油";
//        info.qqNumber = @"456123897";
//        info.web = @"www.baidu.com";
//        info.attribute = ECustomAttribute_1;
//        info.nextVisitTime = 3;
//        info.Memo = @"备注";
//        info.initTime = [NSDate date];
//        info.sourceType = CallSourceType_allocation;
//        info.potentialCustomersTime = 4;
//        info.transactionTime = 4;
//        info.visitCount = 10;
//        info.sumCallTime = 100;
//        info.reVisitTime = 3;
//        [self.followArray addObject:info];
//        
//        info = [[[HCCRMCustomInfo alloc] init] autorelease];
//        info.ID = 105;
//        info.customPhoneNumber = @"23456789";
//        info.mobilePhoneNumber = @"567890";
//        info.phoneNumber = @"678901";
//        info.contact = @"刘大";
//        info.company = @"中国石油";
//        info.qqNumber = @"456123897";
//        info.web = @"www.baidu.com";
//        info.attribute = ECustomAttribute_1;
//        info.nextVisitTime = 3;
//        info.Memo = @"备注";
//        info.initTime = [NSDate date];
//        info.sourceType = CallSourceType_allocation;
//        info.potentialCustomersTime = 4;
//        info.transactionTime = 4;
//        info.visitCount = 10;
//        info.sumCallTime = 100;
//        info.reVisitTime = 3;
//        [self.giveUpArray addObject:info];
    }
    return self;
}

- (void)dealloc
{
    g_defaultModel = nil;
    [[HCNetworkManger shareManger] removeObserver:self];
    
    self.transactionArray = nil;
    self.potentialArray = nil;
    self.followArray = nil;
    self.giveUpArray = nil;
    self.staffID = nil;
    self.workPhoneNumber = nil;
    
    [super dealloc];
}

- (int)allCRMCustomCount
{
    return self.transactionArray.count + self.potentialArray.count + self.followArray.count + self.giveUpArray.count;
}

- (NSArray*)getCustomArrayByType:(ECustomType)type
{
    NSArray* array = nil;
    
    switch (type)
    {
        case ECustomType_Transaction:
        {
            array = self.transactionArray;
            break;
        }
        case ECustomType_Potential:
        {
            array = self.potentialArray;
            break;
        }
        case ECustomType_Follow:
        {
            array = self.followArray;
            break;
        }
        case ECustomType_GiveUp:
        {
            array = self.giveUpArray;
            break;
        }
        default:
            break;
    }
    return array;
}

- (int)sumCallCount
{
    int callCount = 0;
    
    for (ECustomType i = ECustomType_Transaction; i <= ECustomType_GiveUp; i++)
    {
        NSArray* array = [self getCustomArrayByType:i];
        for (HCCRMCustomInfo* info in array)
        {
            callCount += info.visitCount;
        }
    }
    
    return callCount;
}

- (int)sumCallTime
{
    int callTime = 0;
    
    for (ECustomType i = ECustomType_Transaction; i <= ECustomType_GiveUp; i++)
    {
        NSArray* array = [self getCustomArrayByType:i];
        for (HCCRMCustomInfo* info in array)
        {
            callTime += info.sumCallTime;
        }
    }
    
    return callTime;
}

- (void)addCustonInfoByCallInfo:(HCCallInfo*)callInfo
{
    HCCRMCustomInfo* info = [[[HCCRMCustomInfo alloc] init] autorelease];
    info.customPhoneNumber = callInfo.phoneNumber;
    info.mobilePhoneNumber = @"";
    info.phoneNumber = @"";
    info.contact = @"";
    info.company = @"";
    info.qqNumber = @"";
    info.web = @"w";
    info.attribute = ECustomAttribute_1;
    info.nextVisitTime = 0;
    info.Memo = @"";
    info.initTime = callInfo.initTime;
    info.sourceType = callInfo.callSourceType;
    info.potentialCustomersTime = 0;
    info.transactionTime = 0;
    info.visitCount = callInfo.callNumber;
    info.sumCallTime = callInfo.sumCallTime;
    info.reVisitTime = 0;
//    info.remarkInfoList = callInfo.remarkInfoList;
    [self.potentialArray addObject:info];
}

- (void)removeCustomInfoByUserId:(NSString*)userId
{
    for (ECustomType i = ECustomType_Transaction; i <= ECustomType_Follow; i++)
    {
        NSMutableArray* array = nil;
        switch (i)
        {
            case ECustomType_Transaction:
            {
                array = self.transactionArray;
                break;
            }
            case ECustomType_Potential:
            {
                array = self.potentialArray;
                break;
            }
            case ECustomType_Follow:
            {
                array = self.followArray;
                break;
            }
            case ECustomType_GiveUp:
            {
                array = self.giveUpArray;
                break;
            }
            default:
                break;
        }
        for (HCCRMCustomInfo* info in array)
        {
            if ([info.userID isEqualToString:userId])
            {
                [array removeObject:info];
                break;
            }
        }
    }
}

- (void)addCustomInfo:(HCCRMCustomInfo *)customInfo customType:(ECustomType)customType
{
    NSMutableArray* array = nil;
    switch (customType)
    {
        case ECustomType_Transaction:
        {
            array = self.transactionArray;
            break;
        }
        case ECustomType_Potential:
        {
            array = self.potentialArray;
            break;
        }
        case ECustomType_Follow:
        {
            array = self.followArray;
            break;
        }
        case ECustomType_GiveUp:
        {
            array = self.giveUpArray;
            break;
        }
        default:
            break;
    }
    [array addObject:customInfo];
}

- (BOOL)modifyCustomInfo:(HCCRMCustomInfo*)customInfo customID:(NSString*)customID
{
    NSMutableArray* array = nil;
    for (ECustomType type = ECustomType_Transaction; type <= ECustomType_GiveUp; type++)
    {
        switch (type)
        {
            case ECustomType_Transaction:
            {
                array = self.transactionArray;
                break;
            }
            case ECustomType_Potential:
            {
                array = self.potentialArray;
                break;
            }
            case ECustomType_Follow:
            {
                array = self.followArray;
                break;
            }
            case ECustomType_GiveUp:
            {
                array = self.giveUpArray;
                break;
            }
            default:
                break;
        }
        
        int index = 0;
        for (HCCRMCustomInfo* info in array)
        {
            if ([info.userID isEqualToString:customID])
            {
                [array insertObject:customInfo atIndex:index];
                [array removeObject:info];
                return YES;
            }
            index++;
        }
    }
    return NO;
}

- (HCCRMCustomInfo*)getCustomInfoByID:(NSString*)customID customType:(ECustomType)customType;
{
    NSMutableArray* array = nil;
    switch (customType)
    {
        case ECustomType_Transaction:
        {
            array = self.transactionArray;
            break;
        }
        case ECustomType_Potential:
        {
            array = self.potentialArray;
            break;
        }
        case ECustomType_Follow:
        {
            array = self.followArray;
            break;
        }
        case ECustomType_GiveUp:
        {
            array = self.giveUpArray;
            break;
        }
        default:
            break;
    }
    
    for (HCCRMCustomInfo* info in array)
    {
        if ([info.userID isEqualToString:customID])
        {
            return info;
        }
    }
    
    return nil;
}

#pragma mark -- HCNetworkObserver

- (void)notifyNetworkMsg:(int)msgID data:(void*) data
{
    /*pause modify———————————————------
    switch (msgID)
    {
        case EQueryCustStat:
        {
            queryCustStatResp* resp = (queryCustStatResp*)data;
            
            if (!resp->headResp.success)
            {
                //显示错误提示
                break;
            }
            
            //增加处理逻辑
            
            [self sendDataUpdateEvent:msgID];
            break;
        }
        case EQueryCustList:
        {
            queryCustListResp* resp = (queryCustListResp*)data;
            
            if (!resp->headResp.success)
            {
                //显示错误提示
                break;
            }
            
            for (int i = 0; i < resp->vecBean.size(); i++)
            {
                HCCRMCustomInfo* info = [[HCCRMCustomInfo alloc] init];
                
                info.userID = [NSString stringWithFormat:@"%d",resp->vecBean.at(i).custId];
                info.mobilePhoneNumber = [NSString stringWithUTF8String:resp->vecBean.at(i).mobile.data()];
                info.contact = [NSString stringWithUTF8String:resp->vecBean.at(i).name.data()];
                info.company = [NSString stringWithUTF8String:resp->vecBean.at(i).company.data()];
                info.qqNumber = [NSString stringWithUTF8String:resp->vecBean.at(i).qq.data()];
                info.email = [NSString stringWithUTF8String:resp->vecBean.at(i).email.data()];
                info.phoneNumber = [NSString stringWithUTF8String:resp->vecBean.at(i).fax.data()];
                info.sourceType = (CallSourceType)resp->vecBean.at(i).type;
                NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
                [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
                info.initTime = [dateFormatter dateFromString:[NSString stringWithUTF8String:resp->vecBean.at(i).c_time.data()]];
                [dateFormatter release];
                ECustomType customType = (ECustomType)resp->vecBean.at(i).status;
                customType = ECustomType_Follow;
                [self addCustomInfo:info customType:customType];
                
                [info release];
            }
            
            [self sendDataUpdateEvent:msgID];
            break;
        }
        case EQueryCustDetail:
        {
            queryCustDetailResp* resp = (queryCustDetailResp*)data;
            
            if (!resp->headResp.success)
            {
                //显示错误提示
                break;
            }
            
            NSString* customID = [NSString stringWithFormat:@"%d",resp->bean.custId];
            ECustomType customType = (ECustomType)resp->bean.status;
            HCCRMCustomInfo* info = [[self getCustomInfoByID:customID customType:customType] retain];
            
            info.mobilePhoneNumber = [NSString stringWithUTF8String:resp->bean.mobile.data()];
            info.contact = [NSString stringWithUTF8String:resp->bean.name.data()];
            info.company = [NSString stringWithUTF8String:resp->bean.company.data()];
            info.qqNumber = [NSString stringWithUTF8String:resp->bean.qq.data()];
            info.email = [NSString stringWithUTF8String:resp->bean.email.data()];
            info.phoneNumber = [NSString stringWithUTF8String:resp->bean.fax.data()];
            info.sourceType = (CallSourceType)resp->bean.type;
            NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
            [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
            info.initTime = [dateFormatter dateFromString:[NSString stringWithUTF8String:resp->bean.c_time.data()]];
            [dateFormatter release];
            
            [self modifyCustomInfo:info customID:customID];
            
            [info release];
            
            [self sendDataUpdateEvent:msgID];
            break;
        }
    }
     pause modify———————————————————*/
}

@end